package com.github.m6d21.sso.common.util;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 *<p>
 *
 *</p>
 *
 * @author liuyang
 * @date 2018/12/8 Sat 01:02:00
 * @since 1.0.0
 */
public class RadixUtil {

    final static String NAME = RadixUtil.class.getName();

    /**
     * 产生进制的数字
     **/
    final static char[] DIGIT = {//
        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', //
        'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', //
        'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', //
        'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', //
        'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', //
        'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', //
        '8', '9' //
    };

    final static Map<Character, Integer> digitMap = new HashMap<Character, Integer>();

    static {
        for (int i = 0; i < DIGIT.length; i++) {
            digitMap.put(Character.valueOf(DIGIT[i]), Integer.valueOf(i));
        }
    }

    /**
     * 支持的最大进制数
     */
    public static final int MAX_RADIX = DIGIT.length;

    /**
     * 支持的最小进制数
     */
    public static final int MIN_RADIX = 2;

    /**
     * 最多一次返回32个字符
     **/
    public static final int SIZE = 32;

    public static String toString(long i) {
        return toString(i, MAX_RADIX);
    }

    /**
     * 只用26个大写字母
     **/
    public static String toLetter(long i) {
        return toString(i, 26);
    }

    /**
     * 将长整型数值转换为指定的进制数（最大支持62进制）
     */
    public static String toString(long i, int radix) {
        if (radix < MIN_RADIX || radix > MAX_RADIX) {
            radix = 10;
        }
        if (radix == 10) {
            return Long.toString(i);
        }

        StringBuilder sb = new StringBuilder(SIZE);
        boolean negative = (i < 0);
        if (negative) {
            i = -i;
        }

        while (i >= radix) {
            sb.append(DIGIT[(int) (i % radix)]);
            i = i / radix;
        }
        sb.append(DIGIT[(int) (i)]);
        if (negative) {
            sb.append("-");
        }
        return sb.reverse().toString();
    }

    private static NumberFormatException forInputString(String s) {
        return new NumberFormatException("For input string: \"" + s + "\"");
    }

    /**
     * 将字符串转换为长整型数字
     */
    public static long toNumber(String s) {
        return toNumber(s, MAX_RADIX);
    }

    public static long toNumberLetter(String s) {
        return toNumber(s, 26);
    }

    /**
     * 将字符串转换为长整型数字
     */
    public static long toNumber(String s, int radix) {
        if (s == null) {
            throw new NumberFormatException("null");
        }
        if (radix < MIN_RADIX) {
            throw new NumberFormatException("radix " + radix + " less than " + NAME + ".MIN_RADIX");
        }
        if (radix > MAX_RADIX) {
            throw new NumberFormatException("radix " + radix + " greater than " + NAME + ".MAX_RADIX");
        }
        // 返回结果
        long result = 0;
        char[] ss = s.toCharArray();
        // 计算阶乘
        long factorial = 1;
        for (int i = (ss.length - 1); i >= 0; i--) {
            char c = ss[i];
            Integer v = digitMap.get(Character.valueOf(c));
            if (v != null) {
                result += v.intValue() * factorial;
                factorial *= radix;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        UUID uuid = UUID.randomUUID();
        System.out.println(uuid.toString());
        long most = Math.abs(uuid.getMostSignificantBits());
        String mosts, leasts;
        long least = Math.abs(uuid.getLeastSignificantBits());
        StringBuilder sb24 = new StringBuilder();
        sb24 = new StringBuilder();
        mosts = RadixUtil.toLetter(most);
        sb24.append(mosts);
        leasts = RadixUtil.toLetter(least);
        sb24.append(leasts);
        System.out.println(sb24.toString());
        System.out.println("most:\n" + most);
        System.out.println("mosts:\n" + mosts);
        System.out.println("mosts-r:\n" + RadixUtil.toNumberLetter(mosts));
        System.out.println("least:\n" + least);
        System.out.println("leasts:\n" + leasts);
        System.out.println("leasts-r:\n" + RadixUtil.toNumberLetter(leasts));
    }
}
